package com.masterclient.app.modules.shop.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.shop.beans.StoreRecommendBean;
import com.masterclient.app.modules.shop.beans.StoreRecommendGroupBean;

import java.util.List;

/**
 * @auther hjian
 * @date 2020/4/9.
 */
public class StoreRecommendHorizontalLayoutAdapter extends DelegateAdapter.Adapter<MainViewHolder> {
    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int count = 0;
    private String title;
    private List<StoreRecommendBean> data;
    private StoreRecommendAdapter hotsaleAdapter;
    private RecyclerView recyclerView;

    public StoreRecommendHorizontalLayoutAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, StoreRecommendGroupBean data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.count = count;
        this.mLayoutParams = layoutParams;
        this.title = data.getText();
        this.data = data.getProducts();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.store_hotsale, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ((TextView) holder.itemView.findViewById(R.id.store_hotsale_title)).setText(title);
        recyclerView = holder.itemView.findViewById(R.id.store_hotsale_recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        if (recyclerView.getItemDecorationCount() <= 0) {
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int position = parent.getChildAdapterPosition(view);
                    if (position == 0) {
                        outRect.set(0, 0, 0, 0);
                    } else {
                        outRect.set(ScreenUtils.dp2px(10), 0, 0, 0);
                    }

                }
            });
        }

        hotsaleAdapter = new StoreRecommendAdapter(RecyclerView.HORIZONTAL);
        hotsaleAdapter.bindToRecyclerView(recyclerView);
        recyclerView.setAdapter(hotsaleAdapter);
        hotsaleAdapter.setNewData(data);
        countdown();
    }

    private CountDownTimer countDownTimer;

    private void countdown(){
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(Long.MAX_VALUE, 1000) {
                public void onTick(long millisUntilFinished) {
                    for (int i = 0; i < hotsaleAdapter.getData().size(); i++) {
                        StoreRecommendBean bean = hotsaleAdapter.getData().get(i);
                        long time = TextUtils.isEmpty(bean.getCountdown()) ? 0 : Long.parseLong(bean.getCountdown());
                        if (time > 0) {
                            bean.setCountdown(time - 1 + "");
                        }
                    }

                    hotsaleAdapter.notifyItemRangeChanged(0, hotsaleAdapter.getItemCount(), R.id.item_store_hotsale_countdownll); //刷新条目中的指定控件

                }

                public void onFinish() {

                }
            }.start();
        }
    }

    public void removeTimer() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    public void setData(StoreRecommendGroupBean data) {
        removeTimer();
        position = -1;
        this.data = data.getProducts();
        this.title = data.getText();
        notifyDataSetChanged();
    }

    private int position = 0,xOffset = 0;
    @Override
    public void onViewDetachedFromWindow(@NonNull MainViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (position != -1) {
                position = manager.findFirstVisibleItemPosition();
                View view = manager.findViewByPosition(position);
                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                if (view != null) {
                    xOffset = view.getLeft() - lp.leftMargin - ScreenUtils.dp2px(16); //如果你设置了margin则减去
                }
            }else {
                position = 0;
            }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull MainViewHolder holder) {
        super.onViewAttachedToWindow(holder);
            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
            manager.scrollToPositionWithOffset(position, xOffset);

    }


    @Override
    public int getItemCount() {
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        return 2;
    }

}
